package com.feihao.zhou.demo05;

import java.io.*;
import java.util.ArrayList;


/**
 * 序列化多个对象时，建议将多个对象装入集合容器。再将集合序列化。
 */
public class Test01 {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        Student student1 = new Student(78, "Tom");
        Student student2 = new Student(23, "Jery");
        Student student3 = new Student(12, "Jimi");

        //将多个学生对象序列化到本地：
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("newStudent.txt"));
        ArrayList<Student> list = new ArrayList<>();
        list.add(student1);
        list.add(student2);
        list.add(student3);

        oos.writeObject(list);

        oos.close();


        //反序列化：
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("newStudent.txt"));

        Object o = ois.readObject();
        if (o instanceof ArrayList) {
            ArrayList<Student> list1 = (ArrayList<Student>) o;
            for (Student student : list1) {
                System.out.println(student);
            }
        }
    }
}
